<?php

/**
 * This is the model class for table "tbl_catalog_item".
 *
 * The followings are the available columns in table 'tbl_catalog_item':
 * @property integer $id
 * @property string $title
 * @property string $description
 * @property string $code
 * @property integer $type
 * @property integer $state
 * @property integer $view_count
 * @property integer $catalog_id
 * @property integer $user_id
 *
 * The followings are the available model relations:
 * @property TblUser $user
 * @property TblCatalog $catalog
 * @property TblPictures[] $tblPictures
 */
class CatalogItem extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_catalog_item';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, description, code, type, state, view_count, user_id', 'required'),
			array('type, state, view_count, catalog_id, user_id', 'numerical', 'integerOnly'=>true),
			array('title, code', 'length', 'max'=>100),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, title, description, code, type, state, view_count, sale, catalog_id, user_id', 'safe', 'on'=>'search'),
			);
	}

	public function behaviors()
	{
		return array(
			'withRelated'=>array(
				'class'=>'ext.WithRelatedBehavior',
				),
			);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'catalog' => array(self::BELONGS_TO, 'Catalog', 'catalog_id'),
			'pictures' => array(self::MANY_MANY, 'Pictures', 'tbl_pictures_catalog_item(catalog_item_id, picture_id)'),
			);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Title',
			'description' => 'Description',
			'code' => 'Code',
			'type' => 'Type',
			'state' => 'State',
			'view_count' => 'View Count',
			'sale' => 'On Sale',
			'catalog_id' => 'Catalog',
			'user_id' => 'User',
			);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('type',$this->type);
		$criteria->compare('sale',$this->sale);
		$criteria->compare('state',$this->state);
		$criteria->compare('view_count',$this->view_count);
		$criteria->compare('catalog_id',$this->catalog_id);
		$criteria->compare('user_id',$this->user_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return CatalogItem the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	protected function afterSave()
	{
		if($this->state==ConstProductState::Publicated){
			$url = Yii::app()->createAbsoluteUrl('subscribtion/run');
			ERunActions::touchUrl($url, array('catalog_id'=>$this->catalog_id, 'item_id'=>$this->id));
		}
	}
}
